package com.dd.ma;

import java.io.ByteArrayOutputStream;
import java.io.*;
import java.util.zip.GZIPInputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class MaUtil {
	//public static final String FILEPATH = "c:\\";
	public static final String FILEPATH = "D:\\Tomcat 6.0\\webapps\\DD\\ma\\";
	public static final String LONIN_URL = "http://game1-cbt.ma.sdo.com:10001/connect/app/login?cyt=1";
	public static final String MENU_URL = "http://game1-cbt.ma.sdo.com:10001/connect/app/mainmenu?cyt=1";
	public static final String SELECT_URL = "http://game1-cbt.ma.sdo.com:10001/connect/app/menu/fairyselect?cyt=1";
	public static final String FLOOR_URL = "http://game1-cbt.ma.sdo.com:10001/connect/app/exploration/fairy_floor?cyt=1";
	public static final String BATTLE_URL = "http://game1-cbt.ma.sdo.com:10001/connect/app/exploration/fairybattle?cyt=1";
	public static final String BATTLE_LOSE = "http://game1-cbt.ma.sdo.com:10001/connect/app/exploration/fairy_lose?cyt=1";
	public static final String LOSE_URL = "http://game1-cbt.ma.sdo.com:10001/connect/app/exploration/fairy_lose?cyt=1";
	//只打印full_log
	public static void printMsg(String phone,String msg) {
		//System.out.println(msg);
		try{
			File f = new File(FILEPATH+phone+"_full.html");
			if(!f.exists()){
				f.createNewFile();
			}
			FileWriter writer = new FileWriter(FILEPATH+phone+"_full.html", true);
            writer.write(msg+"</br>");
            writer.close();
		}catch(Exception e){
			
		}
	}
	//2个文件都打
	public static void printMsg1(String phone,String msg) {
		//System.out.println(msg);
		try{
			File f = new File(FILEPATH+phone+"_full.html");
			if(!f.exists()){
				f.createNewFile();
			}
			FileWriter writer = new FileWriter(FILEPATH+phone+"_full.html", true);
            writer.write(msg+"</br>");
            writer.close();
            
			f= new File(FILEPATH+phone+".html");
			if(!f.exists()){
				f.createNewFile();
			}
			writer = new FileWriter(FILEPATH+phone+".html", true);
            writer.write(msg+"</br>");
            writer.close();
		}catch(Exception e){
			
		}
		
		//
	}
	public static String getSndaResponseString(InputStream ins) {
		try {
			GZIPInputStream gis = null;
			gis = new GZIPInputStream(ins);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] b = new byte[0x400];
			int n;
			while ((n = gis.read(b)) != -1)
				baos.write(b, 0, n);
			byte[] bb = baos.toByteArray();
			SecretKeySpec keyspec = new SecretKeySpec("rBwj1MIAivVN222b"
					.getBytes(), "AES");
			Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
			c.init(Cipher.DECRYPT_MODE, keyspec);
			byte[] bbb = c.doFinal(bb);
			String str = new String(bbb, "utf-8");
			return str;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String getSndaRequestString(String input) {
		try {
			SecretKeySpec keyspec = new SecretKeySpec("rBwj1MIAivVN222b"
					.getBytes(), "AES");
			Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
			c.init(Cipher.ENCRYPT_MODE, keyspec);
			String output = Base64.encodeBase64String(c.doFinal(input
					.getBytes()));
			if (output.endsWith("=")) {
				output = output.substring(0, output.length() - 1);
			}
			if (output.endsWith("=")) {
				output = output.substring(0, output.length() - 1);
			}
			return output;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return input;
	}
}
